package com.hccl.service.similarity;

import com.hccl.exception.updateException;
import entities.SLUResult;

import java.io.*;
import java.lang.reflect.Array;
import java.util.*;

import static com.hccl.config.Constants.corpusPath;

public class SimilarityUtil {

    Map<String,Set<String>> dictionaryMap = new HashMap<>();

    public SimilarityUtil() throws IOException {
        index();
    }

    public void index() throws IOException {
            String line = "";
            File file = new File(corpusPath);
            if(file.exists()){
                File[] files = file.listFiles();
                for (File oneFile : files){
                    String nodeName = oneFile.getName();
                    InputStreamReader inputStreamReader= new InputStreamReader(new FileInputStream(oneFile),"utf-8");
                    BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
                    while ((line = bufferedReader.readLine()) != null){
                        String userInput = line.replaceAll("[\\pP\\p{Punct}]","").toLowerCase();
                        if (!dictionaryMap.containsKey(line)) {
                            Set<String> nodeSet = new HashSet<>();
                            nodeSet.add(nodeName);
                            dictionaryMap.put(userInput, nodeSet);
                        } else {
                            Set<String> tmpSet = dictionaryMap.get(userInput);
                            tmpSet.add(nodeName);
                            dictionaryMap.put(userInput, tmpSet);
                        }
                    }
                    bufferedReader.close();
                }
            }
    }

    public void updateSimilarityMap() throws updateException{
        dictionaryMap.clear();
        try {
            index();
        } catch (Exception e) {
            throw new updateException("更新SimilarityMap出错。系统尝试自动恢复...");
        }

    }


    public Map<String,Double> search(String queryStr,float nDistancePercentage){

        Map<String,Double> similarResults = new HashMap<>();
        try {
            String tar = queryStr.replaceAll("[\\pP\\p{Punct}]", "");
            EditDistanceComputer EDComputer = new EditDistanceComputer();

            for (Map.Entry<String,Set<String>> entry : dictionaryMap.entrySet()){
                String src = entry.getKey();
                double accu = EDComputer.similarity(src, tar);
                if (accu > nDistancePercentage) {
                    Set<String> similarRes = entry.getValue();
                    if (similarRes.size()> 0) {
                        for (String re : similarRes){//若有多个结果
                            similarResults.put(re, accu);
                        }
                    }
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }

        return similarResults;
    }

    public ArrayList<SLUResult> compute(String input) throws Exception {

        ArrayList<SLUResult> results = new ArrayList<>();
        Map<String,Double> ruleNamesMap = search(input, 0.95f);
        for (String key : ruleNamesMap.keySet()){
            String name = key.split("@")[1];
            String type = key.split("@")[0];
            Double score = ruleNamesMap.get(key);
            results.add(new SLUResult(name, type, score,new HashMap<String, String>()));
        }
        return results;
    }

}
